﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web;
using System.Threading;

namespace chapter4_thread_4
{

    public class NextStep
    {
        private EndEventHandler _nextHandler;


        public NextStep(EndEventHandler handler)
        {
            this._nextHandler = handler;
        }

        public void AsyncCallback(IAsyncResult ar)
        {
            Console.WriteLine("开始管道结束部分任务");
            Console.WriteLine("线程的 Id: {0}", Thread.CurrentThread.ManagedThreadId);
            // 调用实际的结束处理程序
            this._nextHandler(ar);

            Console.WriteLine("继续管道处理过程。");
            Console.WriteLine();
        }
    }

    public class StepDemo
    {
        private BeginEventHandler _beginHandler;
        private EndEventHandler _endHandler;

        public void AddOnWork(BeginEventHandler beginHandler, EndEventHandler endHandler)
        {
            this._beginHandler = beginHandler;
            this._endHandler = endHandler;
        }

        public void DoAsyncWork(object UnUsed)
        {
            // 仅仅需要启动工作，启动之后线程直接结束
            ThreadPool.QueueUserWorkItem(DoAsyncBeginWork);
        }

        private void DoAsyncBeginWork(object unused)
        {
            Console.WriteLine("开始执行管道 开始部分工作。");
            Console.WriteLine("线程的 Id: {0}", Thread.CurrentThread.ManagedThreadId);

            if (this._beginHandler != null)
            {
                NextStep step = new NextStep(this._endHandler);

                // 保存异步的结果用于调用结束处理时使用
                this._beginHandler(this, EventArgs.Empty, step.AsyncCallback, null);
            }
            Console.WriteLine("注意！此时管道的开始部分已经执行完成了");
            Console.WriteLine();
        }
    }

    public class ReadFile
    {
        private byte[] buffer = new byte[2048];
        System.IO.FileStream fs;
        public IAsyncResult BeginReadFile(
                    Object sender,
                    EventArgs e,
                    AsyncCallback cb,
                    Object extraData
                )
        {
            Console.WriteLine("正在执行开始方法！");
            Console.WriteLine("线程的 Id: {0}", Thread.CurrentThread.ManagedThreadId);
            fs = new System.IO.FileStream("../../demo.log", System.IO.FileMode.Open);
            Console.WriteLine();
            return fs.BeginRead(buffer, 0, 2048, cb, null);

        }
        public void EndReadFile(IAsyncResult ar)
        {
            Console.WriteLine("正在执行结束方法！");
            Console.WriteLine("线程的 Id: {0}", Thread.CurrentThread.ManagedThreadId);
            int length = fs.EndRead(ar);
            string message = System.Text.Encoding.UTF8.GetString(buffer, 0, length);
            Console.WriteLine(message);
            Console.WriteLine("结束方法执行完毕！");
            Console.WriteLine();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            StepDemo demo = new StepDemo();

            ReadFile rf = new ReadFile();
            Console.WriteLine("主程序中！");
            Console.WriteLine("线程的 Id: {0}", Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine();
            demo.AddOnWork(rf.BeginReadFile, rf.EndReadFile);
            demo.DoAsyncWork(null);

            // ThreadPool.QueueUserWorkItem(demo2.DoAsyncWork);

            Console.ReadLine();
        }
    }
}
